<template>
  <div>
    <h1>表单验证</h1>
    <hr>
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
      <el-form-item label="活动名称" prop="name">
        <el-input v-model="ruleForm.name"></el-input>
      </el-form-item>

      <el-form-item label="主持人" prop="uname">
        <el-input v-model="ruleForm.uname"></el-input>
      </el-form-item>
      
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">立即创建</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  mounted(){
    console.log("页面加载成功！");
    // 回显数据
    this.$http.get("/api/activity.json")
    .then(r=>{
      console.log(r);
      this.ruleForm = r.data;
    })
  },
  data() {
    return {
      ruleForm: {
        name: '',
        uname:''
      },
      rules: {
        name: [
          { required: true, message: '请输入活动名称', trigger: 'blur' },
          { min: 3, max: 100, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ],
        uname: [
          { required: true, message: '请输入主持人名字', trigger: 'blur' }
        ]
      }
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          // alert('submit!');
          console.log("提交成功！");
          console.log(this.ruleForm);
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs.ruleForm.resetFields();
    }
  }
}
</script>

<style lang="scss" scoped></style>